#include "UsrAI.h"
#include<set>
#include <iostream>
#include<unordered_map>
using namespace std;
tagGame tagUsrGame;
ins UsrIns;
tagInfo info;
const int HOME_MAX=3;
char Mymap[128][128]={0};
int begin_x=0,begin_y=0;
int center_x=0,center_y=0;
bool home_flag=false;

void UsrAI::processData()
{
    info=getInfo();
    //初始化市镇中心坐标
        if(center_x==0 && center_y==0){
            for(tagBuilding &building:info.buildings){
                if(building.Type==BUILDING_CENTER){
                    center_x=building.BlockDR;
                    center_y=building.BlockUR;
                }
            }
        }
    //初始化搜索的起始坐标
        if(begin_x==0 && begin_y==0){
            begin_x=center_x;
            begin_y=center_y;
        }
    //统计当前已建成的房屋数量
        int home_count=0;
        for(tagBuilding &building:info.buildings){
            if(building.Type==BUILDING_HOME){
                home_count++;

            }
        }
    //如果已建数量未达标，继续找空地建造房屋
        if(home_count<HOME_MAX){
            int new_home_x,new_home_y;//新房屋的坐标
     //寻找新的空地（从上次搜索的位置开始，避免重复）
            if(find_block(new_home_x,new_home_y)){
                for(tagFarmer&farmer:info.farmers){
                        if(farmer.NowState==HUMAN_STATE_IDLE){
                        HumanBuild(farmer.SN,BUILDING_HOME,new_home_x,new_home_y);
                        begin_x=new_home_x;
                        begin_y=new_home_y;
                        break;
                        }
                }
            }
        }

    return ;
}
//从市镇中心开始查找2*2的空地，通过引用返回空地左下角坐标x,y
int UsrAI::find_block(int &x,int &y){
    if(info.theMap ==nullptr){
        cout<<"info.theMap is null!"<<endl;
        return 0;
    }
    for(int i = begin_x + 1; i < 128; i++) {
        for (int j = begin_y + 1; j < 128; j++) {
            if (i + 1 < 128 && j + 1 < 128) {
                // 检查四个位置是否都是陆地
                if ((*info.theMap)[i][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i + 1][j].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i][j + 1].type == MAPPATTERN_GRASS &&
                    (*info.theMap)[i + 1][j + 1].type == MAPPATTERN_GRASS) {
                    cout << "找到陆地: " << i << ", " << j << endl;
                // 检查高度是否相等（平地）
                    int height = (*info.theMap)[i][j].height;
                    if ((*info.theMap)[i + 1][j].height == height &&
                     (*info.theMap)[i][j + 1].height == height &&
                     (*info.theMap)[i + 1][j + 1].height == height) {
                // 找到符合条件的2*2空地，返回左下角坐标
                        x = i;
                        y = j;
                        cout << "找到空地: " << x << ", " << y << endl;
                        return 1; // 返回1表示成功找到
                    }
                 }
              }
           }
       }
    return 0; // 返回0表示未找到合适的空地
}



void UsrAI::initMap(){
    for(int i=0;i<128;i++){
        for(int j=0;j<128;j++){
            if ((*info.theMap)[i][j].type == MAPPATTERN_GRASS){
                Mymap[i][j]='0';
            }
            else if((*info.theMap)[i][j].type ==MAPPATTERN_OCEAN){
                Mymap[i][j]='-';
            }
        }
    }
    mark_Resources();
    mark_Buildings();
    mark_Units();
    for(int i=0;i<128;i++){
        for(int j=0;j<128;j++){
            cout<<Mymap[i][j];
        }
        cout<<endl;
    }
}
void UsrAI::mark_Resources(){
    for(tagResource&resources:info.resources){
        if(resources.Type){
            int i=int (resources.BlockDR);
            int j=int (resources.BlockUR);
            Mymap[i][j]='A';
        }

    }
}
void UsrAI::mark_Buildings(){
    for(tagBuilding &building:info.buildings){
        if(building.Type){
            int i=int (building.BlockDR);
            int j=int (building.BlockUR);
            Mymap[i][j]='a';
        }

    }
}
void UsrAI::mark_Units(){
    ;
}

